package com.ego.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.commons.pojo.EasyUIDatagrid;
import com.ego.commons.pojo.EgoResult;
import com.ego.dubbo.service.TbContentDubboService;
import com.ego.pojo.TbContent;
import com.ego.rabbitmq.SendMessage;
import com.ego.service.TbContentService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@Service
public class TbContentServiceImpl implements TbContentService {

    @DubboReference
    private TbContentDubboService tbContentDubboService;


    @Value("${ego.bigad.categoryId}")
    private long categoryId;


    @Value("${ego.updateBigAdCache.exchange}")
    private String exchange;

    @Value("${ego.updateBigAdCache.routingKey}")
    private String routingKey;

    @Autowired
    private SendMessage sendMessage;

    @Override
    public EasyUIDatagrid contentListPage(long categoryId, int page, int rows) {

        PageInfo<TbContent> contentPageInfo = tbContentDubboService.contentQueryList(categoryId, page, rows);


        EasyUIDatagrid easyUIDatagrid = new EasyUIDatagrid(contentPageInfo.getTotal(),contentPageInfo.getList());


        return easyUIDatagrid;
    }

    @Override
    public EgoResult insertSelective(TbContent content) {
        Date date = new Date();
        content.setCreated(date);
        content.setUpdated(date);

        int insert = tbContentDubboService.insert(content);

        //判断当前是否是新增大广告
        if (content.getCategoryId()==categoryId){
            sendMessage.asyncSenderMessage(exchange,routingKey,"新增广告了...");
        }

        if (insert == 1) {
            return EgoResult.ok();
        }else {
            return EgoResult.error("添加失败");
        }
    }

    @Override
    public EgoResult editContent(TbContent content) {
        Date date = new Date();

        content.setUpdated(date);

        //判断当前是否是更改大广告
        if (content.getCategoryId()==categoryId){
            sendMessage.asyncSenderMessage(exchange,routingKey,"更改广告了...");
        }
        int insert = tbContentDubboService.update(content);

        if (insert == 1) {
            return EgoResult.ok();
        }else {
            return EgoResult.error("更改失败");
        }

    }

    @Override
    public EgoResult deleteByIds(List<Long> ids) {

        boolean flag=false;
        List<TbContent> tbContents = tbContentDubboService.selectByIds(ids);
        for (TbContent tbContent : tbContents) {
            if (tbContent.getCategoryId()==categoryId){
                flag=true;
                break;
            }
        }
        try {
            int i = tbContentDubboService.deleteByIds(ids);
            if (i==1){
                if (flag){
                    sendMessage.asyncSenderMessage(exchange,routingKey,"删除广告了...");
                }
                return EgoResult.ok();
            }

        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }
        return EgoResult.error("批量删除失败");
    }
}
